//
//  RankingViewController.m
//  Share_Home_F
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RankingViewController.h"
#import "BaseCollectionViewCell.h"
#import "BaseCollectionModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "AFHTTPRequestOperationManager.h"
#import "PictureScrollViewController.h"
#import "PictureModel.h"
@interface RankingViewController ()
{
    NSInteger number;
}

@property (nonatomic, retain)NSMutableArray *modelArr;
@property (nonatomic, retain)UIPageControl *pageControl;
@end

@implementation RankingViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"排行榜";
//        self.title = @"排行榜";
        number = 1;
        self.modelArr = [NSMutableArray array];
        
    }
    [self dataJSON];
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addFooter];
    
    self.collectionView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [self.collectionView registerClass:[BaseCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    // Do any additional setup after loading the view.
}
- (void)dataJSON
{
    AFHTTPRequestOperationManager *manager= [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSString *str1 = @"http://pic.jiajuol.com/api/iphone/0500/pic_photo.php?act=like_photo_list&page=";
    NSString *str2 = [NSString stringWithFormat:@"%ld&pagesize=24&time=week&v=5.0", number ];
    NSString *str = [NSString stringWithFormat:@"%@%@", str1, str2];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = responseObject;
        
        for (int i = 0; i < arr.count; i++) {
            NSMutableArray *array = arr[i];
            for (int j = 0; j < array.count; j++) {
            NSMutableDictionary *dic = [array objectAtIndex:j];
            PictureModel *pic = [[PictureModel alloc] init];
            [pic setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:pic];
                
            }
            
        }
        NSLog(@"%ld", [self.modelArr count]);
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];
    
}
- (void)addFooter
{
    [self.collectionView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            number++;
            [self dataJSON];
            [self.collectionView reloadData];
            [self.collectionView footerEndRefreshing];
        });
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.modelArr count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath ];
    
    PictureModel *pic = self.modelArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:pic.bimgfile];
#pragma mark- 占位图(直接在sd_setImageWithURL后添加)
    [cell.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"10.jpg"]];
    
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureScrollViewController *picSVC = [[PictureScrollViewController alloc] init];
    picSVC.arr = self.modelArr;
    picSVC.idNum = [self.modelArr[indexPath.item] id];
    
#pragma mark- 动画效果(rippleEffect为水滴)
    CATransition *transition = [CATransition animation];
//动画时长
    transition.duration = 1;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.type = @"rippleEffect";
    transition.subtype = kCATransitionFromTop;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
 
    
    [self.navigationController pushViewController:picSVC animated:YES];
    [picSVC release];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
